qcollect-traits 0.6.0

Traits for being generic over collection-types.

// ++++++++++++++++++++ meta ++++++++++++++++++++ 

pub trait OrderedCollection {}

// TODO how could we use these?
//pub trait Sequenceion {}
//pub trait Setion {}
//pub trait Mapion {}

// ++++++++++++++++++++ capacity ++++++++++++++++++++ 

pub trait Len {
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool { self.len() == 0 }
}

pub trait Capacity: Len {
    fn capacity(&self) -> usize;

    /// TODO naming? `unused_capacity`?
    fn free_space(&self) -> usize { self.capacity() - self.len() }
}

pub trait Reserve: Capacity {
    fn reserve(&mut self, n: usize);
    fn reserve_exact(&mut self, n: usize);

    fn reserve_capacity(&mut self, new_cap: usize){  
        let len = self.len();
        if new_cap > len { return }
        self.reserve(new_cap - len);
    }
    fn reserve_capacity_exact(&mut self, new_cap: usize){ 
        let len = self.len();
        if new_cap > len { return }
        self.reserve_exact(new_cap - len);
    }
}

pub trait ShrinkToFit: Capacity {
    fn shrink_to_fit(&mut self);
}

pub trait Clear {
    fn clear(&mut self);
}

// ++++++++++++++++++++ element access ++++++++++++++++++++ 

pub trait Contains<Q> {
    fn contains(&self, query: Q) -> bool;
}

/// NOTE: Temporary workaround for lack of HK-lifetimes.
pub trait _Get<'a, Q> { 
    type Ret: 'a;
}

pub trait Get<Q>: for<'a> _Get<'a, Q> { 
    fn get<'a>(&'a self, query: Q) -> <Self as _Get<'a, Q>>::Ret;
}

/// NOTE: Temporary workaround for lack of HK-lifetimes.
pub trait _GetMut<'a, Q> { 
    type Ret: 'a;
}

pub trait GetMut<Q>: Get<Q> + for<'a> _GetMut<'a, Q> {
    fn get_mut<'a>(&'a mut self, query: Q) -> <Self as _GetMut<'a, Q>>::Ret;
}

/// NOTE: Temporary workaround for lack of HK-lifetimes.
pub trait _Iterate<'a> {
    type Item = <Self::Iter as Iterator>::Item;
    type Iter: Iterator<Item = Self::Item> + Clone + 'a;
}

pub trait Iterate: for<'a> _Iterate<'a> {
    fn iter<'a>(&'a self) -> <Self as _Iterate<'a>>::Iter;
}

/// NOTE: Temporary workaround for lack of HK-lifetimes.
pub trait _IterateMut<'a> {
    type Item = <Self::IterMut as Iterator>::Item;
    type IterMut: Iterator<Item = Self::Item> + 'a;
}

pub trait IterateMut: Iterate + for<'a> _IterateMut<'a> {
    fn iter_mut<'a>(&'a mut self) -> <Self as _IterateMut<'a>>::IterMut;
}

// ++++++++++++++++++++ modifiers ++++++++++++++++++++ 

pub trait Insert<Val> {
    type Ret;
    fn insert(&mut self, val: Val) -> Self::Ret;
}

pub trait Insert2<Key, Val> {
    type Ret;
    fn insert(&mut self, key: Key, val: Val) -> Self::Ret;
}

// FIXME default impl
/*impl<T: ?Sized, Key, Val> Insert<(Key, Val)> for T
    where T: Insert2<Key, Val> 
{
    type Ret = <Self as Insert2<Key, Val>>::Ret;
    fn insert(&self, ins: (Key, Val)) -> Self::Ret { self.insert(ins.0, ins.1) }
}*/

pub trait Remove<Q> {
    type Ret;
    fn remove(&mut self, query: Q) -> Self::Ret;
}

pub trait PushFront<Val> {
    fn push_front(&mut self, val: Val);
}

pub trait PushBack<Val> {
    fn push_back(&mut self, val: Val);
}

pub trait PopFront {
    type Val;
    fn pop_front(&mut self) -> Option<Self::Val>;
}

pub trait PopBack {
    type Val;
    fn pop_back(&mut self) -> Option<Self::Val>;
}

/* TODO
    truncate
    split_off/at
    split
    rsplit
    front[_mut]
    back[_mut]
    starts_with
    ends_with
    binary_search[_by]
    sort[_by]
    set_all
    
*/